From 66842e08e177e3c458fa0e4db970deae84feb625 Mon Sep 17 00:00:00 2001
From: Jean-Baptiste Kempf <jb@videolan.org>
Date: Tue, 26 May 2015 13:39:00 +0200
Subject: [PATCH 01/13] Fix C++11 compilation of atomic

Close #14569
---
 include/vlc_atomic.h | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

Index: vlc-2.2.3/include/vlc_atomic.h
===================================================================
--- vlc-2.2.3.orig/include/vlc_atomic.h
+++ vlc-2.2.3/include/vlc_atomic.h
@@ -26,13 +26,20 @@
  * Atomic operations do not require locking, but they are not very powerful.
  */
 
-# if !defined (__cplusplus) && (__STDC_VERSION__ >= 201112L) \
-  && !defined (__STDC_NO_ATOMICS__)
+/* Clang older versions support atomics but lacks the stdatomic.h header */
+#if defined(__clang__)
+# if !defined(__has_include) || !__has_include(<stdatomic.h>)
+#  define __STDC_NO_ATOMICS__ 1
+# endif
+#endif
 
+# ifndef __cplusplus
+#  if (__STDC_VERSION__ >= 201112L) && !defined (__STDC_NO_ATOMICS__)
 /*** Native C11 atomics ***/
-#  include <stdatomic.h>
+#   include <stdatomic.h>
 
-# else
+#  else
+/*** Intel/GCC atomics ***/
 
 #  define ATOMIC_FLAG_INIT false
 
@@ -53,22 +60,6 @@
 #  define atomic_is_lock_free(obj) \
     false
 
-/* In principles, __sync_*() only supports int, long and long long and their
- * unsigned equivalents, i.e. 4-bytes and 8-bytes types, although GCC also
- * supports 1 and 2-bytes types. Some non-x86 architectures do not support
- * 8-byte atomic types (or not efficiently). */
-#  if defined (_MSC_VER)
-/* Some atomic operations of the Interlocked API are only
-   available for desktop apps. Thus we define the atomic types to
-   be at least 32 bits wide. */
-typedef      int_least32_t atomic_flag;
-typedef      int_least32_t atomic_bool;
-typedef      int_least32_t atomic_char;
-typedef      int_least32_t atomic_schar;
-typedef     uint_least32_t atomic_uchar;
-typedef      int_least32_t atomic_short;
-typedef     uint_least32_t atomic_ushort;
-#  else
 typedef          bool      atomic_flag;
 typedef          bool      atomic_bool;
 typedef          char      atomic_char;
@@ -76,7 +67,6 @@ typedef   signed char      atomic_schar;
 typedef unsigned char      atomic_uchar;
 typedef          short     atomic_short;
 typedef unsigned short     atomic_ushort;
-#  endif
 typedef          int       atomic_int;
 typedef unsigned int       atomic_uint;
 typedef          long      atomic_long;
@@ -109,10 +99,6 @@ typedef         ptrdiff_t atomic_ptrdiff
 typedef          intmax_t atomic_intmax_t;
 typedef         uintmax_t atomic_uintmax_t;
 
-# if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) || (defined (__clang__) && (defined (__x86_64__) || defined (__i386__)))
-
-/*** Intel/GCC atomics ***/
-
 #  define atomic_store(object,desired) \
     do { \
         *(object) = (desired); \
@@ -203,204 +189,7 @@ typedef         uintmax_t atomic_uintmax
 #  define atomic_flag_clear_explicit(object,order) \
     atomic_flag_clear(object)
 
-# elif defined (__GNUC__)
-
-/*** No atomics ***/
-
-#  define atomic_store(object,desired) \
-    do { \
-        typeof (object) _obj = (object); \
-        typeof (*object) _des = (desired); \
-        vlc_global_lock(VLC_ATOMIC_MUTEX); \
-        *_obj = _des; \
-        vlc_global_unlock(VLC_ATOMIC_MUTEX); \
-    } while (0)
-#  define atomic_store_explicit(object,desired,order) \
-    atomic_store(object,desired)
-
-#  define atomic_load(object) \
-({ \
-    typeof (object) _obj = (object); \
-    typeof (*object) _old; \
-    vlc_global_lock(VLC_ATOMIC_MUTEX); \
-    _old = *_obj; \
-    vlc_global_unlock(VLC_ATOMIC_MUTEX); \
-    _old; \
-})
-#  define atomic_load_explicit(object,order) \
-    atomic_load(object)
-
-#  define atomic_exchange(object,desired) \
-({ \
-    typeof (object) _obj = (object); \
-    typeof (*object) _des = (desired); \
-    typeof (*object) _old; \
-    vlc_global_lock(VLC_ATOMIC_MUTEX); \
-    _old = *_obj; \
-    *_obj = _des; \
-    vlc_global_unlock(VLC_ATOMIC_MUTEX); \
-    _old; \
-})
-#  define atomic_exchange_explicit(object,desired,order) \
-    atomic_exchange(object,desired)
-
-#  define atomic_compare_exchange_strong(object,expected,desired) \
-({ \
-    typeof (object) _obj = (object); \
-    typeof (object) _exp = (expected); \
-    typeof (*object) _des = (desired); \
-    bool ret; \
-    vlc_global_lock(VLC_ATOMIC_MUTEX); \
-    ret = *_obj == *_exp; \
-    if (ret) \
-        *_obj = _des; \
-    else \
-        *_exp = *_obj; \
-    vlc_global_unlock(VLC_ATOMIC_MUTEX); \
-    ret; \
-})
-#  define atomic_compare_exchange_strong_explicit(object,expected,desired,order) \
-    atomic_compare_exchange_strong(object, expected, desired)
-#  define atomic_compare_exchange_weak(object,expected,desired) \
-    atomic_compare_exchange_strong(object, expected, desired)
-#  define atomic_compare_exchange_weak_explicit(object,expected,desired,order) \
-    atomic_compare_exchange_weak(object, expected, desired)
-
-#  define atomic_fetch_OP(object,desired,op) \
-({ \
-    typeof (object) _obj = (object); \
-    typeof (*object) _des = (desired); \
-    typeof (*object) _old; \
-    vlc_global_lock(VLC_ATOMIC_MUTEX); \
-    _old = *_obj; \
-    *_obj = (*_obj) op (_des); \
-    vlc_global_unlock(VLC_ATOMIC_MUTEX); \
-    _old; \
-})
-
-#  define atomic_fetch_add(object,operand) \
-    atomic_fetch_OP(object,operand,+)
-#  define atomic_fetch_add_explicit(object,operand,order) \
-    atomic_fetch_add(object,operand)
-
-#  define atomic_fetch_sub(object,operand) \
-    atomic_fetch_OP(object,operand,-)
-#  define atomic_fetch_sub_explicit(object,operand,order) \
-    atomic_fetch_sub(object,operand)
-
-#  define atomic_fetch_or(object,operand) \
-    atomic_fetch_OP(object,operand,|)
-#  define atomic_fetch_or_explicit(object,operand,order) \
-    atomic_fetch_or(object,operand)
-
-#  define atomic_fetch_xor(object,operand) \
-    atomic_fetch_OP(object,operand,^)
-#  define atomic_fetch_xor_explicit(object,operand,order) \
-    atomic_fetch_sub(object,operand)
-
-#  define atomic_fetch_and(object,operand) \
-    atomic_fetch_OP(object,operand,&)
-#  define atomic_fetch_and_explicit(object,operand,order) \
-    atomic_fetch_and(object,operand)
-
-#  define atomic_flag_test_and_set(object) \
-    atomic_exchange(object, true)
-
-#  define atomic_flag_test_and_set_explicit(object,order) \
-    atomic_flag_test_and_set(object)
-
-#  define atomic_flag_clear(object) \
-    atomic_store(object, false)
-
-#  define atomic_flag_clear_explicit(object,order) \
-    atomic_flag_clear(object)
-
-# elif defined (_MSC_VER)
-
-# include <windows.h>
-
-/*** Use the Interlocked API. ***/
-
-/* Define macros in order to dispatch to the correct function depending on the type.
-   Several ranges are need because some operations are not implemented for all types. */
-#  define atomic_type_dispatch_32_64(operation, object, ...) \
-    (sizeof(*object) == 4 ? operation((LONG *)object, __VA_ARGS__) : \
-    sizeof(*object) == 8 ? operation##64((LONGLONG *)object, __VA_ARGS__) : \
-    (abort(), 0))
-
-#  define atomic_type_dispatch_16_64(operation, object, ...) \
-    (sizeof(*object) == 2 ? operation##16((short *)object, __VA_ARGS__) : \
-    atomic_type_dispatch_32_64(operation, object, __VA_ARGS__))
-
-#  define atomic_type_dispatch_8_64(operation, object, ...) \
-    (sizeof(*object) == 1 ? operation##8((char *)object, __VA_ARGS__) : \
-    atomic_type_dispatch_16_64(operation, object, __VA_ARGS__))
-
-#  define atomic_store(object,desired) \
-    atomic_type_dispatch_16_64(InterlockedExchange, object, desired)
-#  define atomic_store_explicit(object,desired,order) \
-    atomic_store(object, desired)
-
-#  define atomic_load(object) \
-    atomic_type_dispatch_16_64(InterlockedCompareExchange, object, 0, 0)
-#  define atomic_load_explicit(object,order) \
-    atomic_load(object)
-
-#  define atomic_exchange(object,desired) \
-    atomic_type_dispatch_16_64(InterlockedExchange, object, desired)
-#  define atomic_exchange_explicit(object,desired,order) \
-    atomic_exchange(object, desired)
-
-#  define atomic_compare_exchange_strong(object,expected,desired) \
-    atomic_type_dispatch_16_64(InterlockedCompareExchange, object, *expected, desired) == *expected
-#  define atomic_compare_exchange_strong_explicit(object,expected,desired,order) \
-    atomic_compare_exchange_strong(object, expected, desired)
-#  define atomic_compare_exchange_weak(object,expected,desired) \
-    atomic_compare_exchange_strong(object, expected, desired)
-#  define atomic_compare_exchange_weak_explicit(object,expected,desired,order) \
-    atomic_compare_exchange_weak(object, expected, desired)
-
-#  define atomic_fetch_add(object,operand) \
-    atomic_type_dispatch_32_64(InterlockedExchangeAdd, object, operand)
-#  define atomic_fetch_add_explicit(object,operand,order) \
-    atomic_fetch_add(object, operand)
-
-#  define atomic_fetch_sub(object,operand) \
-    atomic_type_dispatch_32_64(InterlockedExchangeAdd, object, -(LONGLONG)operand)
-#  define atomic_fetch_sub_explicit(object,operand,order) \
-    atomic_fetch_sub(object, operand)
-
-#  define atomic_fetch_or(object,operand) \
-    atomic_type_dispatch_8_64(InterlockedOr, object, operand)
-#  define atomic_fetch_or_explicit(object,operand,order) \
-    atomic_fetch_or(object, operand)
-
-#  define atomic_fetch_xor(object,operand) \
-    atomic_type_dispatch_8_64(InterlockedXor, object, operand)
-#  define atomic_fetch_xor_explicit(object,operand,order) \
-    atomic_fetch_sub(object, operand)
-
-#  define atomic_fetch_and(object,operand) \
-    atomic_type_dispatch_8_64(InterlockedAnd, object, operand)
-#  define atomic_fetch_and_explicit(object,operand,order) \
-    atomic_fetch_and(object, operand)
-
-#  define atomic_flag_test_and_set(object) \
-    atomic_exchange(object, true)
-
-#  define atomic_flag_test_and_set_explicit(object,order) \
-    atomic_flag_test_and_set(object)
-
-#  define atomic_flag_clear(object) \
-    atomic_store(object, false)
-
-#  define atomic_flag_clear_explicit(object,order) \
-    atomic_flag_clear(object)
-
-# else
-#  error FIXME: implement atomic operations for this compiler.
-# endif
-# endif
+# endif /* !C11 */
 
 typedef atomic_uint_least32_t vlc_atomic_float;
 
@@ -427,4 +216,9 @@ static inline void vlc_atomic_store_floa
     atomic_store(atom, u.i);
 }
 
+# else /* C++ */
+/*** Native C++11 atomics ***/
+#   include <atomic>
+# endif /* C++ */
+
 #endif
Index: vlc-2.2.3/include/vlc_spu.h
===================================================================
--- vlc-2.2.3.orig/include/vlc_spu.h
+++ vlc-2.2.3/include/vlc_spu.h
@@ -45,9 +45,6 @@ extern "C" {
 
 typedef struct spu_private_t spu_private_t;
 
-/* Default subpicture channel ID */
-#define SPU_DEFAULT_CHANNEL (1)
-
 /**
  * Subpicture unit descriptor
  */
Index: vlc-2.2.3/include/vlc_vout_osd.h
===================================================================
--- vlc-2.2.3.orig/include/vlc_vout_osd.h
+++ vlc-2.2.3/include/vlc_vout_osd.h
@@ -26,12 +26,13 @@
 #ifndef VLC_VOUT_OSD_H
 #define VLC_VOUT_OSD_H 1
 
-#include <vlc_spu.h>
-
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+//* Default subpicture channel ID */
+#define SPU_DEFAULT_CHANNEL (1)
+
 /**
  * OSD menu position and picture type defines
  */
Index: vlc-2.2.3/src/video_output/video_output.c
===================================================================
--- vlc-2.2.3.orig/src/video_output/video_output.c
+++ vlc-2.2.3/src/video_output/video_output.c
@@ -43,6 +43,7 @@
 #include <vlc_vout.h>
 
 #include <vlc_filter.h>
+#include <vlc_spu.h>
 #include <vlc_vout_osd.h>
 #include <vlc_image.h>
 
Index: vlc-2.2.3/modules/gui/qt4/dialogs/messages.hpp
===================================================================
--- vlc-2.2.3.orig/modules/gui/qt4/dialogs/messages.hpp
+++ vlc-2.2.3/modules/gui/qt4/dialogs/messages.hpp
@@ -28,8 +28,8 @@
 #include "util/singleton.hpp"
 #include "ui/messages_panel.h"
 #include <stdarg.h>
-#include <vlc_atomic.h>
 #include <QMutex>
+#include <QAtomicInt>
 
 class QTabWidget;
 class QPushButton;
@@ -55,7 +55,7 @@ private:
     void sinkMessage( const MsgEvent * );
     bool matchFilter( const QString& );
 
-    atomic_uint verbosity;
+    QAtomicInt verbosity;
     static void MsgCallback( void *, int, const vlc_log_t *, const char *,
                              va_list );
 
Index: vlc-2.2.3/modules/gui/qt4/dialogs/messages.cpp
===================================================================
--- vlc-2.2.3.orig/modules/gui/qt4/dialogs/messages.cpp
+++ vlc-2.2.3/modules/gui/qt4/dialogs/messages.cpp
@@ -143,7 +143,7 @@ MessagesDialog::~MessagesDialog()
 
 void MessagesDialog::changeVerbosity( int i_verbosity )
 {
-    atomic_store( &this->verbosity, i_verbosity );
+    verbosity = i_verbosity;
 }
 
 void MessagesDialog::updateConfig()
@@ -337,7 +337,7 @@ void MessagesDialog::MsgCallback( void *
 {
     MessagesDialog *dialog = (MessagesDialog *)self;
     char *str;
-    int verbosity = atomic_load( &dialog->verbosity );
+    int verbosity = dialog->verbosity;
 
     if( verbosity < 0 || verbosity < (type - VLC_MSG_ERR)
      || unlikely(vasprintf( &str, format, ap ) == -1) )
